package lib
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	
	/**
	 * 显示帧频功能类
	 */
	public class SampleStats extends Sprite
	{
		/// 构造函数
		public function SampleStats()
		{
			addEventListener(Event.ADDED_TO_STAGE, setup);
		}
		
///////////////////////////////////////////////////////////////////////////////////////////////////
// 私有域
///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/// 添加到舞台, 初始化
		private function setup(e : Event) : void
		{
			removeEventListener(Event.ADDED_TO_STAGE, setup);
			
			_info = new TextField();
			_info.selectable = false;
			_info.width = 120;
			_info.height = 20;
			_info.backgroundColor = 0xffffff;
			_info.background = true;
			addChild(_info);
			
			addEventListener(Event.ENTER_FRAME, update);
		}
		
		/// 每帧更新事件
		private function update(e : Event) : void
		{
			_nowFrameTime = getTimer();
			if (_nowFrameTime - _lastFrameTime >= 1000)
			{
				_frame = _frameCount;
				_frameCount = 0;
				_lastFrameTime = _nowFrameTime;
				_nowFrameTime = 0;
			}
			else
			{
				_frameCount++;
			}
			
			_info.text = "FPS:" + _frame;
		
		}

		private var _frame				: uint = 0;
		private var _lastFrameTime		: uint = 0;
		private var _nowFrameTime		: uint = 0;
		private var _frameCount			: uint = 0;
		private var _info				: TextField;
	}
}